package com.pigs.pigscheckinginsystem.service.impl;

import com.mongodb.client.result.UpdateResult;
import com.pigs.pigscheckinginsystem.entity.CheckingIn;
import com.pigs.pigscheckinginsystem.service.CheckingInService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author PIGS
 * @since 2020-04-22
 */
@Service
public class CheckingInServiceImpl implements CheckingInService {

    @Autowired
    private MongoTemplate mongoTemplate;

    private Logger logger = LoggerFactory.getLogger(CheckingInServiceImpl.class);

    /**
     * 签到
     *
     * @param checkingIn
     * @return
     */
    @Override
    public UpdateResult punchIn(CheckingIn checkingIn) {
        logger.info("checkingIn serviceImpl打印 ={}", checkingIn);
        Query query = new Query(Criteria.where("userName").is(checkingIn.getUserName()));
        Update update = new Update().set("punchInTime", checkingIn.getPunchInTime()).set("attendanceTime", checkingIn.getAttendanceTime()).set("remark", checkingIn.getRemark()).set("punchOutTime", checkingIn.getPunchOutTime());
        /**
         * 更新查询返回结果集的第一条
         */
        UpdateResult result = mongoTemplate.updateFirst(query, update, CheckingIn.class, "checkingIn");
        logger.info("result={}", result);

        return result;
    }

    /**
     * 查询用户是否存在
     *
     * @param userName
     * @return
     */
    @Override
    public CheckingIn queryUser(String userName) {
        if (userName != null) {
            Query query = new Query(Criteria.where("userName").is(userName));
            CheckingIn checkingIn = mongoTemplate.findOne(query, CheckingIn.class, "checkingIn");
            return checkingIn;
        }

        return null;
    }

    /**
     * 签退
     *
     * @param checkingIn
     * @return
     */
    @Override
    public CheckingIn punchOut(CheckingIn checkingIn) {


        return null;
    }

    /**
     * 是否迟到如果迟到就需要调用次方法
     *
     * @param checkingIn
     * @return
     */
    @Override
    public CheckingIn userLate(CheckingIn checkingIn) {

        return null;
    }

    /**
     * 查询未签到的同学
     *
     * @return
     */
    @Override
    public List<CheckingIn> queryNoCheckingIn() {

        /**
         * 实现从mongodb把全部的数据取出来
         * 然后获取数组中的数据，数组中的获取签到数据如果有数据返回那就说明已经签到了
         * 再次查询mongodb通过签到的时间来查询
         * 如果没有数据那就说明没有签到
         */
        List<CheckingIn> checkingIn = mongoTemplate.findAll(CheckingIn.class, "checkingIn");
        List<CheckingIn> checkingIn1 = null;

        if (checkingIn.size() > 0 && checkingIn != null) {
            List<String> punchInTimes = checkingIn.stream().map(CheckingIn::getPunchInTime).collect(Collectors.toList());
            List<String> punchOutTimes = checkingIn.stream().map(CheckingIn::getPunchOutTime).collect(Collectors.toList());

            logger.info("punchInTimes={}", punchInTimes);
            for (String punchInTime : punchInTimes) {
                Query query = new Query(Criteria.where("punchInTime").is(punchInTime));
                checkingIn1 = mongoTemplate.find(query, CheckingIn.class, "checkingIn");
            }
            for (CheckingIn in : checkingIn1) {
                logger.info("in={}", in);
            }

        }
        if (checkingIn1 == null) {
            return null;
        }
        return checkingIn1;
    }

    /**
     * 查询已经签到的同学
     *
     * @return
     */
    @Override
    public List<CheckingIn> queryCheckingIn() {
        List<CheckingIn> checkingIn = mongoTemplate.findAll(CheckingIn.class, "checkingIn");
        List<CheckingIn> checkingIn1 = null;
        if (checkingIn.size() > 0 && checkingIn != null) {
            Query query = new Query(Criteria.where("punchInTime").exists(true).ne(null));
            checkingIn1 = mongoTemplate.find(query, CheckingIn.class, "checkingIn");

            for (CheckingIn in : checkingIn1) {
                logger.info("in={}", in);
            }

        }
        return checkingIn1;
    }

}
